/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datasource;

import datasource.bmp.BitmapCoreHeader;
import datasource.bmp.BitmapFileHeader;
import datasource.bmp.ImagenBMP;
import datasource.pixel.MonoPixel;
import datasource.pixel.Pixel;
import datasource.util.BitWise;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Fabian
 */
public class ImageWriter extends FileWriter {

    public static ImagenBMP createRandomMonoImage(int width, int height, String filename) {

        ImagenBMP imagen = null;
        ColorTable ctable;
        Pixel[] pixelarray;
        try {
            //header:
            BitmapFileHeader bmfheader = new BitmapFileHeader();
            //bmcheader:
            BitmapCoreHeader bmcheader = new BitmapCoreHeader(width, height, 1);

            //como es monocromatica, le pongo 1
            int bitspp = 1;
            imagen = new ImagenBMP();
            //escritura de imagen
            //inicializacion de variables y clases
            int rowsize = (int) Math.floor(((bitspp * width) / 32) * 4);
            int pixelarraysize = rowsize * height;
            long bitmapoffset = BitmapFileHeader.HEADER_SIZE + BitmapCoreHeader.HEADER_SIZE;
            /**/
            //revisar esta linea
            /**/
            bmfheader.setFilesize((long) (bitmapoffset
                    + 4 * Math.pow(2, bitspp) + pixelarraysize));
            ctable = new ColorTable(1);
            //inicializacion del pixelarray y pading (acolchado del tamaño de cada rila a multiplo de 4)
            pixelarray = new Pixel[(rowsize + (rowsize % 4)) * height];
            //generacion aleatoria del pixelarray
            for (int i = 0; i < pixelarray.length; i++) {
                pixelarray[i] = new MonoPixel((byte) (Math.random() * ctable.getColorPalleteSize()));
            }

            //apertura del fichero de escritura
            FileOutputStream fileoutput = null;
            try {
                fileoutput = new FileOutputStream(filename);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ImagenBMP.class.getName()).log(Level.SEVERE, null, ex);
            }
            BufferedOutputStream bufferoutput = new BufferedOutputStream(fileoutput);
            //escritura de bmcheader (BitmapCoreHeader)
            bufferoutput.write(BitmapFileHeader.HEADER_FIELD);

            bufferoutput.write(BitWise.longToByteArrayLE(bmfheader.getFilesize()));
            bufferoutput.write(BitWise.intToByteArrayLE(bmfheader.getCreator1()));
            bufferoutput.write(BitWise.intToByteArrayLE(bmfheader.getCreator2()));
            bufferoutput.write(BitWise.longToByteArrayLE(bitmapoffset));

            // Cierre de los ficheros
            bufferoutput.close();
        } catch (IOException ex) {
            Logger.getLogger(ImageWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return imagen;
    }
}
